package {{ package }}.web.controller;

import com.pactera.jep.core.exception.ServiceException;
import com.pactera.jep.web.code.WebCode;
import com.pactera.jep.web.code.WebHeaderCode;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

import com.pactera.jep.orm.Filter;
import com.pactera.jep.orm.Order;
import com.pactera.jep.orm.Page;


/**
 * @Description:  《替换》
 * @author: 周茂恩
 * @Date: {{ date }}
 */
@Api(tags = "《替换》")
@RestController
@RequestMapping("/{{ small_class_name }}")
public class {{ class_name }}Controller extends BaseRestController<{{ class_name }}> {

    private static Logger logger = LoggerFactory.getLogger({{ class_name }}Controller.class);


    @Autowired
    private {{ class_name }}Service {{ small_class_name }}Service;


    @ApiIgnore
	protected void prepareQuery(@ApiIgnore @RequestHeader(WebHeaderCode.X_USER_TOKEN) String token, Page<{{ class_name }}> page, List<Filter> filters, List<Order> orders, HttpServletRequest request) {
		areaInfoService.query(page, filters, orders);
	}


    @ApiIgnore
    @Override
    public Map<String, Object> query(String token, Page<{{ class_name }}> page, Model model, HttpServletRequest request) {
        return super.query(token, page, model, request);
    }


    @GetMapping(value = "/index")
    public String index(){
        return "测试页, 删除";
    }


    @ApiOperation(value = "读取指定ID的数据，将结果放在rows中返回", response = {{ class_name }}.class)
    @GetMapping("/getInfo")
    public Map<String, Object> getInfo(@RequestParam("id") String id) {
        Map<String, Object> tips = new HashMap<String, Object>();

        try {
			List<{{ class_name }}> list = new ArrayList<{{ class_name }}>();
            {{ class_name }} {{ small_class_name }} = {{ small_class_name }}Service.get(id);

			list.add({{ small_class_name }});
			tips.put(RESULT_ROWS, list);
            tips.put(WebCode.SUCCESS, true);
            tips.put(WebCode.MSG, WebCode.SUCCESS_OPERATE);
        } catch (ServiceException e) {
			logger.error("error:",e);
            tips.put(WebCode.SUCCESS, false);
            tips.put(WebCode.MSG, WebCode.FAILURE_OPERATE);
        }
        return tips;
    }


	@ApiOperation(value = "保存一条数据到数据库中")
	@PostMapping(value = "/save")
	public Map<String, Object> save(@RequestBody {{ class_name }} {{ small_class_name }}) {
		Map<String, Object> tips = new HashMap<String, Object>();

		try {
			boolean	 exists = {{ small_class_name }}Service.exists(new Filter.Builder().eq("id", {{ small_class_name }}.getId()).build());
			if(exists) {
				{{ small_class_name }}Service.update({{ small_class_name }});
				tips.put(WebCode.SUCCESS, true);
				tips.put(WebCode.MSG, WebCode.SUCCESS_SAVE);
			} else {
				{{ small_class_name }}Service.insert({{ small_class_name }});
				tips.put(WebCode.SUCCESS, true);
				tips.put(WebCode.MSG, WebCode.SUCCESS_SAVE);
			}
		} catch (ServiceException e) {
			logger.error("error:",e);
            tips.put(WebCode.SUCCESS, false);
            tips.put(WebCode.MSG, WebCode.FAILURE_OPERATE);
		}
		return tips;
	}


    @ApiOperation(value = "根据编码删除一条数据")
	@DeleteMapping(value = "/remove/{id}")
	public Map<String, Object> remove(@PathVariable String id){
		Map<String, Object> tips = new HashMap<String, Object>();
		try {
			{{ small_class_name }}Service.deleteByPrimaryKey(id);
			tips.put(WebCode.SUCCESS, true);
			tips.put(WebCode.MSG, WebCode.SUCCESS_DELETE);
		} catch (ServiceException e) {
			logger.error("error:",e);
			tips.put(WebCode.SUCCESS, false);
			tips.put(WebCode.MSG, WebCode.FAILURE_DELETE);
		}
		return tips;
	}


    @ApiOperation(value = "根据编码批量删除数据")
	@PostMapping(value = "/removeBatch")
	public Map<String, Object> removeBatch(@RequestBody Map<String, Object> param){
		Map<String, Object> tips = new HashMap<String, Object>();
		try {
			housesInfoService.deleteByFilter(new Filter.Builder().in("id", param.get("ids")).build());
			tips.put(WebCode.SUCCESS, true);
			tips.put(WebCode.MSG, WebCode.SUCCESS_DELETE);
		} catch (ServiceException e) {
			logger.error("error:",e);
			tips.put(WebCode.SUCCESS, false);
			tips.put(WebCode.MSG, WebCode.FAILURE_DELETE);
		}
		return tips;
	}



}